package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.dto.csunbutton.BedOldmanCsunButtonReqDto;
import com.csun.cmny.provider.model.dto.sleep.BedOldmanSleepReqDto;
import com.csun.cmny.provider.model.dto.watch.OldmanBindWatchReqDto;
import com.csun.cmny.provider.model.vo.*;
import com.csun.cmny.provider.model.vo.csunsleep.DmcCsunSleepVo;
import com.csun.cmny.provider.model.vo.csunsleep.SleepAndAlert;
import com.csun.cmny.provider.service.*;
import com.csun.cmny.provider.vo.RoomSleepInfo;
import com.csun.cmny.provider.vo.SleepDataVo;
import com.csun.cmny.util.wrapper.Wrapper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class DmcRpcServiceImpl implements DmcRpcService {

    @Resource
    private DmcDeviceFeignApi dmcDeviceFeignApi;

    @Resource
    private DmcWatchDataFeignApi dmcWatchDataFeignApi;

    @Resource
    private DmcWatchOldmanFeignApi dmcWatchOldmanFeignApi;

    @Resource
    private DmcCsunSleepDataFeignApi dmcCsunSleepDataFeignApi;

    @Resource
    private DmcSleepCareDataFeignApi dmcSleepCareDataFeignApi;

    @Resource
    private DmcDeviceAlertFeignApi dmcDeviceAlertFeignApi;

    //@Override
    //public DeviceReqVo getDeviceByid(Long deviceId) {
      /*  Wrapper<DeviceReqDto> wrapper =  dmcDeviceFeignApi.getDeviceById(deviceId);
        DeviceReqVo deviceReqVo = new DeviceReqVo();
        BeanUtils.copyProperties( wrapper.getResult(),deviceReqVo);
        return  deviceReqVo;*///return  null;
    //}

    /**
     * query watch oldman address by oldman id list
     *
     * @param oldmanIds oldman id list
     * @return the list.
     */
    @Override
    public List<WatchOldmanAddressVo> queryWatchOldmanAddressByOldmanIds(List<Integer> oldmanIds) {

        try {
            Wrapper<List<WatchOldmanAddressVo>> wrapper = dmcWatchDataFeignApi.queryWatchOldmanAddressByOldmanIds(oldmanIds);

            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610045);
            }

            List<WatchOldmanAddressVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryWatchOldmanAddressByOldmanIds = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query watch oldman bing watch list
     *
     * @param oldmanBindWatchReqDto oldman not bind watch req dto
     * @return the list.
     */
    @Override
    public List<Integer> queryOldmanBindWatchList(OldmanBindWatchReqDto oldmanBindWatchReqDto) {

        try {
            Wrapper<List<Integer>> wrapper = dmcWatchOldmanFeignApi.queryOldmanBindWatchList(oldmanBindWatchReqDto);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610046);
            }

            List<Integer> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryOldmanBindWatchList = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query body sensor location by room id list
     *
     * @param roomIdList room id list
     * @return the list.
     */
    @Override
    public List<BodySensorReqVo> queryBodySensorLocationByRoomIds(List<Integer> roomIdList) {

        try {
            Wrapper<List<BodySensorReqVo>> wrapper = dmcDeviceFeignApi.queryBodySensorLocationByRoomIds(roomIdList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610050);
            }

            List<BodySensorReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryBodySensorLocationByRoomIds = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query csun sleep data by bed oldman list
     *
     * @param bedOldmanSleepReqDtoList bed oldman sleep req dto list
     * @return the list.
     */
    @Override
    public List<CsunSleepDataReqVo> queryCsunSleepDataByBedOldmanList(List<BedOldmanSleepReqDto> bedOldmanSleepReqDtoList) {

        try {
            Wrapper<List<CsunSleepDataReqVo>> wrapper = dmcCsunSleepDataFeignApi.queryCsunSleepDataByBedOldmanList(bedOldmanSleepReqDtoList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610051);
            }

            List<CsunSleepDataReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryCsunSleepDataByBedOldmanList = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query sleep care data by bed oldman list
     *
     * @param bedOldmanSleepReqDtoList bed oldman sleep req dto list
     * @return the list.
     */
    @Override
    public List<SleepCareDataReqVo> querySleepCareDataByBedOldmanList(List<BedOldmanSleepReqDto> bedOldmanSleepReqDtoList) {
        try {
            Wrapper<List<SleepCareDataReqVo>> wrapper = dmcSleepCareDataFeignApi.querySleepCareDataByBedOldmanList(bedOldmanSleepReqDtoList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610051);
            }

            List<SleepCareDataReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("querySleepCareDataByBedOldmanList = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query csun button by bed oldman list
     *
     * @param bedOldmanCsunButtonReqDtoList bed oldman csun button req dto list
     * @return the list.
     */
    @Override
    public List<CsunButtonReqVo> queryCsunButtonByBedOldmanList(List<BedOldmanCsunButtonReqDto> bedOldmanCsunButtonReqDtoList) {

        try {
            Wrapper<List<CsunButtonReqVo>> wrapper = dmcDeviceFeignApi.queryCsunButtonByBedOldmanList(bedOldmanCsunButtonReqDtoList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610052);
            }

            List<CsunButtonReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryCsunButtonByBedOldmanList = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query rfid label by oldman id list
     *
     * @param oldmanIdList oldman id list
     *
     * @return the list.
     */
    @Override
    public List<RfidLabelReqVo> queryRfidLabelByOldmanIds(List<Integer> oldmanIdList) {

        try {
            Wrapper<List<RfidLabelReqVo>> wrapper = dmcDeviceFeignApi.queryRfidLabelByOldmanIds(oldmanIdList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610053);
            }

            List<RfidLabelReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryRfidLabelByOldmanIds = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * query device alert by device list
     *
     * @param deviceReqVoList device req vo list
     *
     * @return
     */
    @Override
    public List<DeviceAlertReqVo> queryDeviceAlertByDeviceList(List<com.csun.cmny.provider.model.vo.DeviceReqVo> deviceReqVoList) {

        try {
            Wrapper<List<DeviceAlertReqVo>> wrapper = dmcDeviceAlertFeignApi.queryDeviceAlertByDeviceList(deviceReqVoList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610053);
            }

            List<DeviceAlertReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryDeviceAlertByDeviceList = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * send building floor update msg
     *
     * @param tagList tag list
     */
    @Override
    public void sendBuildingFloorUpdateMsg(List<String> tagList) {

        try {
            Wrapper<Integer> wrapper = dmcDeviceAlertFeignApi.sendBuildingFloorUpdateMsg(tagList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610056);
            }

        } catch (Exception e) {
            log.error("sendBuildingFloorUpdateMsg = {}", e.getMessage(), e);
        }

    }

    @Override
    public List<DeviceAlertReqVo> queryDeviceAlertByOldIdAndDate(DeviceAlertReqVo deviceAlertReqVo) {
        try {
            Wrapper<List<DeviceAlertReqVo>> wrapper = dmcDeviceAlertFeignApi.queryDeviceAlertByOldIdAndDate(deviceAlertReqVo);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610056);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("queryDeviceAlertByOldIdAndDate = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public List<DeviceCountVo> queryDeviceNumByGroupByDeviceType(Long groupId) {
        try {
            Wrapper<List<DeviceCountVo>> wrapper = dmcDeviceFeignApi.queryDeviceNumByGroupByDeviceType(groupId);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610057);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("queryDeviceNumByGroupByDeviceType = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Integer breakBindDeviceByOldmanId(Long oldmanId) {
        try {
            Wrapper<Integer> wrapper = dmcDeviceFeignApi.breakBindDeviceByOldmanId(oldmanId);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC10067016);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("breakBindDeviceByOldmanId = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Map selectDeviceDataByRoomId(Long roomId) {
        try {
            Wrapper<Map> wrapper = dmcDeviceFeignApi.selectDeviceDataByRoomId(roomId);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("selectDeviceDataByRoomId = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public Map selectAlertDataByRoomId(Long roomId,Long groupId) {
        try {
            Wrapper<Map> wrapper = dmcDeviceFeignApi.selectAlertDataByRoomId(roomId,groupId);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615004);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("selectAlertDataByRoomId = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public List<String> getCameraDeviceNoByLifeRoomId(Long roomId) {
        try {
            Wrapper<List<String>> wrapper = dmcDeviceFeignApi.getCameraDeviceNoByLifeRoomId(roomId);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC10068005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("getCameraDeviceNoByLifeRoomId = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public IsInRoomVO getIsInRoomStatus(Long roomId) {
        try {
            Wrapper<IsInRoomVO> wrapper = dmcDeviceFeignApi.getIsInRoomStatus(roomId);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100614003);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("getIsInRoomStatus = {}", e.getMessage(), e);
        }
        return null;
    }

    @Override
    public List<DeviceReqVo> listDevice(List deviceIdList) {
        try {
            Wrapper<List<DeviceReqVo>> wrapper = dmcDeviceFeignApi.listDevice(deviceIdList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100614003);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("listDevice = {}", e.getMessage(), e);
        }
        return null;
    }

    /**
     * 根据房间Id查询温湿度计数据
     *
     * @param roomIdList the room id list
     *
     * @return the humidistat req vo list
     */
    @Override
    public List<HumidistatReqVo> queryHumidistatByRoomIds(List<Integer> roomIdList) {

        try {
            Wrapper<List<HumidistatReqVo>> wrapper = dmcDeviceFeignApi.queryHumidistatByRoomIds(roomIdList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610050);
            }

            List<HumidistatReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryHumidistatByRoomIds = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 根据房间Id查询摄像头数据
     *
     * @param roomIdList the room id list
     *
     * @return the camera req vo list
     */
    @Override
    public List<CameraReqVo> queryCameraByRoomIds(List<Integer> roomIdList) {

        try {
            Wrapper<List<CameraReqVo>> wrapper = dmcDeviceFeignApi.queryCameraByRoomIds(roomIdList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610050);
            }

            List<CameraReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryCameraByRoomIds = {}", e.getMessage(), e);
        }

        return null;
    }

    /**
     * 根据房间Id查询网关
     *
     * @param roomIdList the room id list
     *
     * @return the gateway req vo
     */
    @Override
    public List<GatewayReqVo> queryGatewayByRoomIds(List<Integer> roomIdList) {

        try {
            Wrapper<List<GatewayReqVo>> wrapper = dmcDeviceFeignApi.queryGatewayByRoomIds(roomIdList);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990002);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100610050);
            }

            List<GatewayReqVo> result = wrapper.getResult();

            return result;
        } catch (Exception e) {
            log.error("queryGatewayByRoomIds = {}", e.getMessage(), e);
        }

        return null;
    }
    //查询床垫数据
    @Override
    public List<Map> querySleepInfoByRoomId(ArrayList<Long> ids) {
        try {
            Wrapper<List<Map>> wrapper = dmcDeviceFeignApi.querySleepInfoByRoomId(ids);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("querySleepInfoByRoomId = {}", e.getMessage(), e);
        }
        return null;
    }
    //查询报警信息
    @Override
    public PageInfo querySleepAlertInfo(ArrayList<Long> ids,Integer isHandle,Integer pageNum,Integer pageSize) {
        try {
            Wrapper<PageInfo> wrapper = dmcDeviceFeignApi.querySleepAlertInfo(ids,isHandle,pageNum,pageSize);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("querySleepAlertInfo = {}", e.getMessage(), e);
        }
        return null;
    }
    //查询床垫状态
    @Override
    public List<com.csun.cmny.provider.vo.SleepDataVo> queryInBed(List<Long> deviceIds) {
        try {
            Wrapper<List<SleepDataVo>> wrapper = dmcDeviceFeignApi.queryInBed(deviceIds);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("SleepDataVo = {}", e.getMessage(), e);
        }
        return null;
    }
    //查询绑定了床垫的老人
    @Override
    public List<RoomSleepInfo> querySleepBind(ArrayList<Long> ids) {
        try {
            Wrapper<List<RoomSleepInfo>> wrapper = dmcDeviceFeignApi.querySleepBind(ids);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("RoomSleepInfo = {}", e.getMessage(), e);
        }
        return null;
    }
    //查询床垫时间段是否在床
    @Override
    public List<CsunSleepDataReqVo> querySleepInbed(ArrayList<Long> ids,String time) {
        try {
            Wrapper<List<CsunSleepDataReqVo>> wrapper = dmcDeviceFeignApi.querySleepInbed(ids,time);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("querySleepInbed = {}", e.getMessage(), e);
        }
        return null;
    }
    //查询报警信息
    @Override
    public List<DmcDeviceAlert> queryAllSleepAlert(ArrayList<Long> ids) {
        try {
            Wrapper<List<DmcDeviceAlert>> wrapper = dmcDeviceFeignApi.queryAllSleepAlert(ids);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("querySleepInbed = {}", e.getMessage(), e);
        }
        return null;
    }

    //查询床垫绑定
    @Override
    public List<DmcCsunSleepVo> queryGroupCsunSleepList(Long groupId) {
        try {
            Wrapper<List<DmcCsunSleepVo>> wrapper = dmcDeviceFeignApi.queryGroupCsunSleepList(groupId);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("queryGroupCsunSleepList = {}", e.getMessage(), e);
        }
        return null;
    }
    //查询床垫绑定以及老人基本信息
    @Override
    public SleepAndAlert queryGroupCsunSleepListByGroupId(Long groupId) {
        try {
            Wrapper<SleepAndAlert> wrapper = dmcDeviceFeignApi.queryGroupCsunSleepListByGroupId(groupId);
            if (wrapper == null) {
                throw new DmcBizException(ErrorCodeEnum.GL99990100);
            }
            if (wrapper.error()) {
                throw new DmcBizException(ErrorCodeEnum.DMC100615005);
            }
            return wrapper.getResult();
        } catch (Exception e) {
            log.error("queryGroupCsunSleepListByGroupId = {}", e.getMessage(), e);
        }
        return null;
    }
}
